/*
 * socketObject.h
 *
 *  Created on: 14.03.2009
 *      Author: Christian Stupp
 */

#ifndef SOCKETOBJECT_H_
#define SOCKETOBJECT_H_

#if defined __linux__
typedef int SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR   -1
#define PHOSTENT	hostent*
#define ioctlsocket(a,b,c) ioctl(a,b,c)
#endif

using namespace std;

#include<string>
#ifdef _WIN32
#include <WinSock2.h>
#else
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#endif

#ifndef _WIN32
#define Sleep(a) sleep(a/1000)
#endif

#include<iostream>
#include <string.h>
#include "compressObject.h"
#include "cryptObject.h"
#include "debuger.h"

namespace oal {

	enum TypeSocket {BlockingSocket, NonBlockingSocket};

	class socketServer;

	class socketObject {
	private:
		bool compressStart;
		compressObject *compress;
		bool encryptStart;
		cryptObject *encrypt;
	public:
		string remoteAddress;
		virtual ~socketObject();
		socketObject(const socketObject&);
		socketObject& operator=(socketObject&);

		std::string ReceiveLine();
		std::string ReceiveBytes();
		std::string ReceiveClearBytes();
		std::string ReceiveAll();

		// The parameter of SendLine is not a const reference
		// because SendLine modifes the std::string passed.
		void   SendLine (std::string);

		// The parameter of SendBytes is a const reference
		// because SendBytes does not modify the std::string passed
		// (in contrast to SendLine).
		void   SendBytes(const std::string&);
		int	   SendClearBytes(const std::string&);

		void   Close();
		std::string		getIp(void);


		void startCompress(void);
		void destroyCompress(void);

		void startEncrypt(std::string method, std::string password);
		void destroyEncrypt(void);
	protected:
		friend class socketServer;
	//  friend class SocketSelect;

		socketObject(SOCKET s);
		socketObject();

	#ifdef _WIN32
	  SOCKET s_;
	#else
	  int s_;
	#endif

	  int* refCounter_;

	private:
	  static void Start();
	  static void End();
	  static int  sockets;
	};

};

#endif /* SOCKETOBJECT_H_ */
